﻿using System;
using System.Web.UI;
using OctoOS;
using OctoOS.UI;
using System.Web;

/// <summary>
/// Summary description for ArticleUIPreview
/// </summary>
public class MyArticleUIPreview : Image48ObjectPreview// : BaseArticleUIPreview
{
	MyPageArticlePreview _preview;
	Category[] _categories;

	public MyArticleUIPreview(MyPageArticlePreview preview, Category[] categories)
	{
		_categories = categories;
		_preview = preview;
		HeaderText = preview.Title;
		ImageUrl = "/images/article.gif";
		HeaderUrl = Urls.GetEditArticle(preview.ID);
	}

	protected override void RenderBodyContent(HtmlTextWriter writer)
	{
		base.RenderBodyContent(writer);

		writer.Write("<div>");
		writer.Write("<span> " + Resources.Main.myArticles_NumberOfPages + ": "
			+ _preview.PagesCount);
 		writer.Write("</span>");

		if(_preview.NonCompletedPages.Length > 0)
		{
			writer.Write(" | <span");
			writer.WriteAttribute("style", "color:red");
			writer.Write(">");
			writer.Write(Resources.Main.myArticles_NonCompleted + ": " + _preview.NonCompletedPages.Length);
 			writer.Write("</span>");
		}

		if(_preview.PagesCount > 0)
		{
			writer.Write(" | <a ");
			writer.WriteAttribute("href", Urls.GetArticle(_preview.ID, true, true, _preview.Title));
			writer.Write(">" + Resources.Main.shared_ViewArticle + "</a>");
		}

		writer.Write("</div>");
	}

	protected override void RenderFooterContent(HtmlTextWriter writer)
	{
		string categoryNames = string.Empty;

		foreach(var categoryID in _preview.CategoryIDs)
		{
			var name = AppUtil.GetCategory(_categories, categoryID).Title;
			if(!String.IsNullOrEmpty(categoryNames))
				categoryNames += ", ";

			categoryNames += name;
		}

		writer.Write("<span>");
		writer.Write(Resources.Main.shared_Categories + ": " + HttpUtility.HtmlEncode(categoryNames));
		writer.Write("</span><br/>");

		writer.Write(Resources.Main.myArticles_Created + " ");

		var createTime = new Time(_preview.CreateTime.ToLocalTime());
		createTime.RenderControl(writer);
		writer.Write("<br/>");

		writer.Write("<span");
		bool published = //_preview.Published &&
			_preview.PublishStartTime <= DateTime.UtcNow
			&& _preview.PublishStopTime > DateTime.UtcNow;

		
		if(!published)
			writer.WriteAttribute("style", "color:red");
		writer.Write(">");


		if(published)
		{
			writer.Write(Resources.Main.shared_Published + " ");
			var t = new Time(_preview.PublishStartTime.ToLocalTime());
			t.RenderControl(writer);
		}
		else
		{
			writer.Write(Resources.Main.myArticles_NotPublished);

			if(_preview.PublishStartTime != DateTime.MaxValue)
			{
				writer.Write("<br/>");
				writer.Write(Resources.Main.shared_PublishStarts + " ");

				var pubStartTime = new Time(_preview.PublishStartTime.ToLocalTime());
				pubStartTime.RenderControl(writer);
			}
		}

		if(_preview.PublishStopTime != DateTime.MaxValue)
		{
			writer.Write("<br/>");
			writer.Write(Resources.Main.shared_PublishStops + " ");

			var pubStopTime = new Time(_preview.PublishStopTime.ToLocalTime());
			pubStopTime.RenderControl(writer);
		}

		writer.Write("</span>");
	}
}